<%@page import="java.awt.print.Printable"%>
<%@ page contentType="text/html;charset=UTF-8"%>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core"%>
<%@ taglib prefix="shiro" uri="http://shiro.apache.org/tags"%>
<%@ taglib prefix="fn" uri="http://java.sun.com/jsp/jstl/functions"%>
<%@ taglib prefix="tags" tagdir="/WEB-INF/tags"%>
<c:set var="ctx" value="${pageContext.request.contextPath}" />

<html>
<head>
<title>_盘点清单</title>
<script type="text/javascript">

    
     $(function() {
	      var status='${task.status}';
	      if(status=='undo' || status=='finish'){
	      
	        $("#save_btn").attr("disabled", true);
	        $("#save_btn").attr("class", "btn");
	        
		    $("#finish_btn").attr("disabled", true);
		    $("#finish_btn").attr("class", "btn");
		    
	      }
     });

   //保存
   function save(){
   
      var items=new Array();
      var flag=true;
      
      $("input[name='itemData']").each(function(){
	         var id=$(this).attr("id");
	         var tr_flag=true;
	         var sales_id="#sales_"+id;
	         var unused_id="#unused_"+id;
	         var order_id="#order_"+id;
	         
	         var sales_msg_id="#sales_msg_"+id;
	         var unused_msg_id="#unused_msg_"+id;
	         var order_msg_id="#order_msg_"+id;
	         
	         var tr_id="#tr_"+id;
	
			 var sales_num=$(sales_id).val().trim();
			 if(isNaN(sales_num) ){
			     $(sales_msg_id).html("请输入有效的数字");
			     flag=false;
			     tr_flag=false;
			 }else{
			     $(sales_msg_id).html("");
			 }
			 
			 
			 var unused_num=$(unused_id).val().trim();
			 if(isNaN(unused_num) ){
			     $(unused_msg_id).html("请输入有效的数字");
			     flag=false;
			     tr_flag=false;
			 }else{
			     $(unused_msg_id).html("");
			 }
			 
			 var order_num=$(order_id).val().trim();
			 if(isNaN(order_num) ){
			     $(order_msg_id).html("请输入有效的数字");
			     flag=false;
			     tr_flag=false;
			 }else{
			     $(order_msg_id).html("");
			 }
			 
			 if(tr_flag == false){
			   $(tr_id).css("background-color",'yellow');
			 }else{
			   $(tr_id).css("background-color",'');
			 }
			 
			 
			 var item=createItem(id,sales_num,unused_num,order_num);
			 items.push(item);
		          
      });
      
      if(flag == false){
         alert("数据有误，请检查核对...");
         return ;
      }
      
      var jsonList = eval(items); 
	  var jsonText = JSON.stringify(jsonList);
	   
	  $.ajax({
			type:'post',
			url:'${ctx}/checkCountTask/ajaxSaveCountData',
			data:{
			    taskCode:'${task.taskCode}',
				jsonText:jsonText
			},
			dataType:'json',
			success:function(data) {
			    if(data==true){
			      alert("已经保存数据！");
			    }else{
			      alert("网络繁忙，请稍后重试...");
			    }
			},
			error:function() {
				alert("网络繁忙，请稍后重试...");
			}
	  });
          
   }
   
   
   //创建json对象  
   function createItem(id,salesNum,unusedNum,orderNum){
	    return {
	        id:id,
	        salesNum:salesNum,
	        unusedNum:unusedNum,
	        orderNum:orderNum
	    };
	}
	
	//完成任务
	function finish(){
	
	      var items=new Array();
      var flag=true;
      
      $("input[name='itemData']").each(function(){
	         var id=$(this).attr("id");
	         var tr_flag=true;
	         var sales_id="#sales_"+id;
	         var unused_id="#unused_"+id;
	         var order_id="#order_"+id;
	         
	         var sales_msg_id="#sales_msg_"+id;
	         var unused_msg_id="#unused_msg_"+id;
	         var order_msg_id="#order_msg_"+id;
	         
	         var tr_id="#tr_"+id;
	
			 var sales_num=$(sales_id).val().trim();
			 if(isNaN(sales_num) ){
			     $(sales_msg_id).html("请输入有效的数字");
			     flag=false;
			     tr_flag=false;
			 }else{
			     $(sales_msg_id).html("");
			 }
			 
			 var unused_num=$(unused_id).val().trim();
			 if(isNaN(unused_num) ){
			     $(unused_msg_id).html("请输入有效的数字");
			     flag=false;
			     tr_flag=false;
			 }else{
			     $(unused_msg_id).html("");
			 }
			 
			 var order_num=$(order_id).val().trim();
			 if(isNaN(order_num) ){
			     $(order_msg_id).html("请输入有效的数字");
			     flag=false;
			     tr_flag=false;
			 }else{
			     $(order_msg_id).html("");
			 }
			 
			 if(tr_flag == false){
			   $(tr_id).css("background-color",'yellow');
			 }else{
			   $(tr_id).css("background-color",'');
			 }
			 
			 var item=createItem(id,sales_num,unused_num,order_num);
			 items.push(item);
      });
      
      if(flag == false){
         alert("数据有误，请检查核对...");
         return ;
      }
      
      var jsonList = eval(items); 
	  var jsonText = JSON.stringify(jsonList);
	  
	  var r=confirm("确定保存，并完成此任务吗？");
	  
	  if(r==false){
   		return;
      }else{
	        $.ajax({
				type:'post',
				url:'${ctx}/checkCountTask/ajaxFinish',
				data:{
				    taskCode:'${task.taskCode}',
					jsonText:jsonText
				},
				dataType:'json',
				success:function(data) {
				    if(data==true){
				         location.href='${ctx}/checkCountTask/v2_queryTaskList';
				    }else{
				      alert("网络繁忙，请稍后重试...");
				    }
				},
				error:function() {
					alert("网络繁忙，请稍后重试...");
				}
		  });
      }
      
	   
	}


</script>
</head>

<body>
<section class="content">

<div class="box box-solid box-primary" >
     <div class="box-header"> <i class="fa fa-th-large"></i> <h3 class="box-title">盘点点数</h3> </div> 
	<div class="box-body">

	<table id="stockCodeTable" class="table table-bordered table-condensed" >
		<thead><tr>
		    <th >序号</th>
		    <th >商家名称</th>
		    <th >仓库网点</th>
		    <th >库位编码</th>
		    <th >箱编码</th>
		    <th >SKU编码</th>
		    <th >批次号</th>
		    <th >生产日期</th>
		    <th >过期日期</th>
		    
		    <th >可销售</th>
		    <th >不可销售</th>
		    <th >订单占用</th>
		    
		    <th >可销售(点数)</th>
		    <th >不可销售(点数)</th>
		    <th >订单占用(点数)</th>
		    
		</tr></thead>
		
	    <c:forEach items="${itemList}" var="item" varStatus="i">
				<tr id="tr_${item.id}" >
				   <td>${i.count}</td>
				   <td>${item.customer.customerName}</td>
				   <td>${item.warehouse.groupName}</td>
				   <td>${item.stockCode}</td>
				   <td>${item.boxCode}</td>
				   <td>${item.sku}</td>
				   <td>${item.batchCode}</td>
				   <td>${item.productDate}</td>
				   <td>${item.expireDate}</td>
				   
				   <td>${item.salesNum}</td>
				   <td>${item.unusedNum}</td>
				   <td>${item.orderNum}</td>
				   
				   <td>
				     <input type="hidden" name="itemData" id="${item.id}">
				     <input id="sales_${item.id}" type="text" value="${item.salesNumCheck }"/>
				     <span  id='sales_msg_${item.id}' style='color:red;'></span>
				   </td>
				   <td>
				     <input id="unused_${item.id}" type="text" value="${item.unusedNumCheck}"/>
				     <span   id='unused_msg_${item.id}' style='color:red;'></span>
				   </td>
				    <td>
				     <input id="order_${item.id}" type="text" value="${item.orderNumCheck}" readonly="readonly"/>
				     <span   id='order_msg_${item.id}' style='color:red;'></span>
				   </td>
				</tr>
		</c:forEach>
	 
	 </table>
	 
	 </div>
	 
	 </div>

      	<div class="form-actions">
      	    <input id="save_btn" class="btn btn-primary" type="button" value="保存" onclick="save();" />
      	    <input id="finish_btn" class="btn btn-danger" type="button" value="完成点数" onclick="finish();" />
	    </div>
</section>
</body>
</html>
